package com.wh1200.mybatis.service

import com.wh1200.mybatis.xmlless.inter.ITenantProvider
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler
import net.sf.jsqlparser.expression.Expression
import net.sf.jsqlparser.expression.LongValue
import net.sf.jsqlparser.expression.StringValue

/**
 *
 * @author 吴昊
 * @date 2023/12/21 9:38
 * @since v4.0.0
 * @version 1.0
 */
class XmllessTenantLineHandler(private val provider: ITenantProvider<*>) : TenantLineHandler {

  override fun getTenantId(): Expression {
    return when (val id = provider.getTenantId()) {
      is String -> StringValue(id)
      is Int    -> LongValue(id.toLong())
      is Long   -> LongValue(id)
      is Short  -> LongValue(id.toLong())
      is Byte   -> LongValue(id.toLong())
      else      -> StringValue(id.toString())
    }
  }

  override fun ignoreTable(tableName: String): Boolean {
    return tableName != "t_student"
  }

}
